﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KPIBalanceScoreCard
{
    public partial class KPIGroupList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!"".Equals(itemGroupName.Value))
            {

                if (((Hierarchy)this.Master).hierarchyTreeView.SelectedValue != null)
                {
                    try
                    {
                        Session[ApplicationConstants.ORGANIZATION_NAME.ToString()] = ((Hierarchy)this.Master).hierarchyTreeView.SelectedValue.Split(new String[] { "@@!!" }, StringSplitOptions.None)[0].Trim().Split(new String[] { "@@" }, StringSplitOptions.None)[0];


                        if (((Hierarchy)this.Master).hierarchyTreeView.SelectedValue != null && ((Hierarchy)this.Master).hierarchyTreeView.SelectedValue.Contains(ApplicationStrings.ROLE_PREFIX))
                        {
                            String role = ((Hierarchy)this.Master).hierarchyTreeView.SelectedValue.Split(new String[] { ApplicationStrings.ROLE_PREFIX }, StringSplitOptions.None)[1].Trim();

                            Session[ApplicationConstants.SELECTED_ROLE.ToString()] = role;
                            Session[ApplicationConstants.SELECTED_EMPNAME.ToString()] = null;
                        }
                        else if (((Hierarchy)this.Master).hierarchyTreeView.SelectedValue != null && ((Hierarchy)this.Master).hierarchyTreeView.SelectedValue.Contains(ApplicationStrings.EMP_NAME_PREFIX))
                        {
                            String empName = ((Hierarchy)this.Master).hierarchyTreeView.SelectedValue.Split(new String[] { ApplicationStrings.EMP_NAME_PREFIX }, StringSplitOptions.None)[1].Trim();

                            Session[ApplicationConstants.SELECTED_ROLE.ToString()] = null;
                            Session[ApplicationConstants.SELECTED_EMPNAME.ToString()] = empName;
                            Session[ApplicationConstants.CHOOSEN_NODE_VALUE.ToString()] = empName;


                        }

                    }
                    catch
                    { }
                }
                
                Session[ApplicationConstants.SELECTED_ITEM_GROUP.ToString()] = itemGroupName.Value;
                //Session[ApplicationConstants.SELECTED_ROLE.ToString()]

                HierarchyDataset.ItemDetailsDataTable detailsDtMTD = new HierarchyDataset.ItemDetailsDataTable();
                HierarchyDataset.ItemDetailsDataTable detailsDtYTD = new HierarchyDataset.ItemDetailsDataTable();

                try
                {
                    if (Session[ApplicationConstants.SELECTED_ROLE.ToString()] != null && Roles.BM.ToString().Equals(Session[ApplicationConstants.SELECTED_ROLE.ToString()]))
                    {
                        detailsDtMTD = new HierarchyDatasetTableAdapters.ItemDetailsTableAdapter().GetItemDetailsForBM(ApplicationConstants.MTD.ToString(), itemGroupName.Value, Double.Parse(yearDropDown.Text), Double.Parse(monthDropDown.Text), Session[ApplicationConstants.ORGANIZATION_NAME.ToString()].ToString());
                        detailsDtYTD = new HierarchyDatasetTableAdapters.ItemDetailsTableAdapter().GetItemDetailsForBM(ApplicationConstants.YTD.ToString(), itemGroupName.Value, Double.Parse(yearDropDown.Text), Double.Parse(monthDropDown.Text), Session[ApplicationConstants.ORGANIZATION_NAME.ToString()].ToString());

                    }
                    else if (Session[ApplicationConstants.SELECTED_ROLE.ToString()] != null && !Roles.BM.ToString().Equals(Session[ApplicationConstants.SELECTED_ROLE.ToString()]))
                    {
                        detailsDtMTD = new HierarchyDatasetTableAdapters.ItemDetailsTableAdapter().GetItemDetailsByRole(Session[ApplicationConstants.SELECTED_ROLE.ToString()].ToString(), ApplicationConstants.MTD.ToString(), itemGroupName.Value, Double.Parse(yearDropDown.Text), Double.Parse(monthDropDown.Text), Session[ApplicationConstants.ORGANIZATION_NAME.ToString()].ToString());
                        detailsDtYTD = new HierarchyDatasetTableAdapters.ItemDetailsTableAdapter().GetItemDetailsByRole(Session[ApplicationConstants.SELECTED_ROLE.ToString()].ToString(), ApplicationConstants.YTD.ToString(), itemGroupName.Value, Double.Parse(yearDropDown.Text), Double.Parse(monthDropDown.Text), Session[ApplicationConstants.ORGANIZATION_NAME.ToString()].ToString());
                    }
                    else if (Session[ApplicationConstants.SELECTED_EMPNAME.ToString()] != null)
                    {
                        detailsDtMTD = new HierarchyDatasetTableAdapters.ItemDetailsTableAdapter().GetItemDetailsByEmpName(Session[ApplicationConstants.SELECTED_EMPNAME.ToString()].ToString(), ApplicationConstants.MTD.ToString(), itemGroupName.Value, Double.Parse(yearDropDown.Text), Double.Parse(monthDropDown.Text), Session[ApplicationConstants.ORGANIZATION_NAME.ToString()].ToString());
                        detailsDtYTD = new HierarchyDatasetTableAdapters.ItemDetailsTableAdapter().GetItemDetailsByEmpName(Session[ApplicationConstants.SELECTED_EMPNAME.ToString()].ToString(), ApplicationConstants.YTD.ToString(), itemGroupName.Value, Double.Parse(yearDropDown.Text), Double.Parse(monthDropDown.Text), Session[ApplicationConstants.ORGANIZATION_NAME.ToString()].ToString());
                    }
                }
                catch { }

                Session[ApplicationConstants.ITEM_DETAILS_MTD.ToString()] = detailsDtMTD;
                Session[ApplicationConstants.ITEM_DETAILS_YTD.ToString()] = detailsDtYTD;
                itemGroupName.Value = "1.Business Target";
            }
        }

        protected void showAllDetails_Click(object sender, EventArgs e)
        {

        }

        protected void yearDropDown_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session[ApplicationConstants.CHOOSEN_YEAR.ToString()] = yearDropDown.SelectedValue;
            Session[ApplicationConstants.REFRESH_TREE.ToString()] = true;
            itemGroupName.Value = "1.Business Target";

        }

        protected void monthDropDown_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session[ApplicationConstants.CHOOSEN_MONTH.ToString()] = monthDropDown.SelectedValue;
            
            Session[KPIBalanceScoreCard.ApplicationConstants.ITEM_GROUPS.ToString()] = new KPIBalanceScoreCard.HierarchyDataset.ItemGroupsDataTable();
            Session[ApplicationConstants.ITEM_DETAILS_MTD.ToString()] = new HierarchyDataset.ItemDetailsDataTable();
            Session[ApplicationConstants.ITEM_DETAILS_YTD.ToString()] = new HierarchyDataset.ItemDetailsDataTable();


            Session[ApplicationConstants.TREE_STRUCTURE.ToString()] = null;
            Session[ApplicationConstants.REFRESH_TREE.ToString()] = true;
            itemGroupName.Value = "1.Business Target";
        }
    }
}
